package com.atm.bulksms;

import java.util.Enumeration;
import java.util.Vector;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.atm.bulksms.data.ContactItem;
import com.atm.bulksms.util.Constants;
import com.atm.bulksms.util.Utils;

public class RecipientListView {
	
	//Context ctx;
	LayoutInflater inflater;
	RadioGroup group ;
	TextView recipentView;
	boolean groupVisible;
	
	public RecipientListView(final Activity ctx, int resourceId) {
		//this.ctx = ctx;
		inflater = LayoutInflater.from(ctx);
		LinearLayout layout = (LinearLayout)ctx.findViewById(resourceId);
		System.out.println(" layout "+layout);
		recipentView = (TextView)layout.findViewById(R.id.recipients_count_text);
		group = (RadioGroup)layout.findViewById(R.id.recipients_list);
		
		ImageView addView = (ImageView)layout.findViewById(R.id.recipients_add);
		addView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				ctx.showDialog(Constants.DIALOG_CONTACT_PICK_LIST);
			}});
		
	}
	

	public  void loadSelectedContacts() {
		group.removeAllViews();
		updateRecipientView();
		Enumeration<String> groups = Utils.getSelectdGroupNames();
		while(groups.hasMoreElements()){
			String groupName = groups.nextElement();
			addSelectedItem(groupName, null);
		}

		System.out.println("After Adding Groups:: child count == "+group.getChildCount());
		Enumeration<ContactItem> items = Utils.getSelectedContacts().elements();
		System.out.println(" Number of selected Contacts "+items);
		while(items.hasMoreElements()){
			ContactItem item = items.nextElement();
			Vector<String> numberList = item.getNumberList();
			for(int i = 0; i<numberList.size(); i++){
				final String no = numberList.elementAt(i);
				// if not part of any selected group then only add the contacts in view
				//if(!Utils.isContactInSelectedGroup(item.getName(), no)){
					addSelectedItem(item.getName(), no);
				//}
				
			}
		}
	}
	private void updateRecipientView() {
		recipentView.setText("("+Utils.getSelectedRecipientCount()+") Recipients Added");
	}
	
	private void addSelectedItem(final String name, final String number) {
		final RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.group_contact_item, null);
		TextView nameView = (TextView)layout.findViewById(R.id.group_contact_item_name);
		String str = name;
		if(number != null) str+= "\n"+number;
		nameView.setText(str);
		group.addView(layout);
		ImageView btn = (ImageView) layout.findViewById(R.id.group_contact_item_btn);
		btn.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				group.removeView(layout);
				if(number == null){
					Utils.removeSelectedGroup(name);
					updateRecipientView();
				}else{
					int id = Utils.getContactId(v.getContext(), number);
					Utils.removeContact(id);
					//removedContacts.add(number);
				}
			}});
		System.out.println(" Added "+group.getChildCount());
	}
	
	public void onToggle(View v){
		System.out.println(" recipients view toogled");
		groupVisible = !groupVisible;//(v.getVisibility()== View.GONE) ? false : true; // current status
		System.out.println("groupVisible "+groupVisible);
		if(groupVisible)
			group.setVisibility(View.GONE); //8
		else{
			group.setVisibility(View.VISIBLE); //0
			group.setAnimation(AnimationUtils.loadAnimation(v.getContext(),
                    R.anim.push_up_in));
			//group.setAnimation(AnimationUtils.makeInChildBottomAnimation(v.getContext()));
		}
		System.out.println(" New Status : v.getVisibility() "+v.getVisibility());
		System.out.println(" count "+group.getChildCount());
		group.invalidate();
	}

	public int getCount(){
		return group.getChildCount();
	}
	

	
}
